home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue57 / Str2Date / TestDate2Str.dpr < prev    next >
Encoding:
Text File  |  2000-01-30  |  3.0 KB  |  108 lines

  1. program TestDate2Str;
  2.  
  3. uses
  4.   SysUtils,
  5.   Dialogs,
  6.   Str2Date;
  7.  
  8. var
  9.   bDayB4Month:boolean;
  10.   TotalTests: integer;
  11.   TotalFails: integer;
  12.  
  13. procedure ExtractDate(const DateValue: TDateTime; const DateText: string; const DayB4Month: boolean);
  14. begin
  15.   try
  16.     Inc(TotalTests);
  17.     if DateValue <> StrToDateNew(DateText, DayB4Month) then
  18.       Raise Exception.CreateFmt('Incorrect QDate from Str2Date(%s, ..)', [DateText]);
  19.   except
  20.     on E: Exception do
  21.       begin
  22.       MessageDlg(E.Message, mtError, [mbOk], 0);
  23.       Inc(TotalFails);
  24.       end;
  25.   end;
  26. end;
  27.  
  28. procedure FunctionalTests;
  29. var
  30.   DateText: string;
  31.   DateValue: TDateTime;
  32.   iYear, iMonth, iDay: integer;
  33. begin
  34.  
  35.   ExtractDate(3, '1900\1\2', False);          // YMD
  36.   ExtractDate(3, '1900/2/1', True);           // YDM
  37.   ExtractDate(3, '2-1-1900', True);           // DMY
  38.   ExtractDate(3, '1.2.1900', False);          // MDY
  39.  
  40.   ExtractDate(3, '  1900 / 1 / 2  ', False);  // YMD and multiple separators
  41.   ExtractDate(3, '  1900 / 2 / 1  ', True);   // YDM and multiple separators
  42.   ExtractDate(3, '  2 \ 1 \ 1900  ', True);   // DMY and multiple separators
  43.   ExtractDate(3, '  1 \ 2 \ 1900  ', False);  // MDY and multiple separators
  44.  
  45.   ExtractDate(3, '  1900  2  Jan  ', True);   // YDM Month Name and multiple separators
  46.   ExtractDate(3, '  1900  Jan  2  ', False);  // YMD Month Name and multiple separators
  47.   ExtractDate(3, '  2  Jan  1900  ', True);   // DMY Month Name and multiple separators
  48.   ExtractDate(3, '  Jan  2  1900  ', False);  // MDY Month Name and multiple separators
  49.  
  50.   for iYear := 1900 to 2200 do
  51.     for iMonth := 1 to 12 do
  52.       for iDay := 1 to 2 do
  53.         begin
  54.         DateValue := EncodeDate(iYear, iMonth, iDay);
  55.         DateText  := DateToStr(DateValue);
  56.         ExtractDate(DateValue, DateText, bDayB4Month);
  57.         end;
  58. end;
  59.  
  60. procedure PerformanceTest;
  61. var
  62.   iLoop: integer;
  63.   Factor: double;
  64.   DateText: string;
  65.   BegTime, EndTime: TDateTime;
  66.   DelphiElapsed, HelperElapsed: TDateTime;
  67. const
  68.   LoopMax = 1000000;
  69. begin
  70.  
  71.   DateText := DateToStr(EncodeDate(2000, 1, 1));
  72.  
  73.   BegTime := Time;
  74.   for iLoop := 1 to LoopMax do
  75.     StrToDate(DateText);
  76.   EndTime := Time;
  77.   DelphiElapsed := EndTime - BegTime;
  78.  
  79.   BegTime := Time;
  80.   for iLoop := 1 to LoopMax do
  81.     StrToDateNew(DateText, bDayB4Month);
  82.   EndTime := Time;
  83.   HelperElapsed := EndTime - BegTime;
  84.  
  85.   Factor := DelphiElapsed / HelperElapsed;
  86.   DateText := Format('StrToDateNew was %2.2g times' +
  87.                      #10'faster than StrToDate!', [Factor]);
  88.   MessageDlg(DateText, mtInformation, [mbOk], 0);
  89. end;
  90.  
  91. begin
  92.   TotalTests := 0;
  93.   TotalFails := 0;
  94.  
  95.   bDayB4Month := True;
  96.   // Delphi date/string routines hate short dates with a month name.
  97.   ShortDateFormat := 'dd' + DateSeparator + 'mm' + DateSeparator + 'yyyy';
  98.  
  99.   FunctionalTests;
  100.  
  101.   MessageDlg(IntToStr(TotalTests) + ' Tests Performed', mtInformation, [mbOk], 0);
  102.   MessageDlg(IntToStr(TotalFails) + ' Failures', mtWarning, [mbOk], 0);
  103.  
  104.   if TotalFails = 0 then PerformanceTest;
  105.  
  106. end.
  107.  
  108.